const Express = require("express");
const RecordServer = require("../../servers/RecordInfo");
const ResponseHelper = require("../ResponseHelper");
const router = Express.Router();

/**
 * 同步病人端数据
 */
router.post("/synchronization", async (req, res) => {
  try {
    const result = await RecordServer.synchronization(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 获取所有记录
 */
router.get("/getAllRecordInfo", async (req, res) => {
  try {
    const result = await RecordServer.getAll();
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 添加记录
 */
router.post("/addNewRecordInfo", async (req, res) => {
  try {
    const result = await RecordServer.add(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 批量添加记录
 */
router.post("/bulkCreateRecord", async (req, res) => {
  try {
    const result = await RecordServer.bulkCreate(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 更新记录
 */
router.post("/updateRecordInfo/:id", async (req, res) => {
  const userId = req.params.id;
  const userObj = req.body;
  try {
    const result = await RecordServer.update(userId, userObj);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 根据id获取记录
 */
router.get("/getRecordInfo/:id", async (req, res) => {
  try {
    const userId = req.params.id;
    const result = await RecordServer.getById(userId);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 根据id删除记录
 */
router.post("/delRecordInfo/:id", async (req, res) => {
  try {
    const userId = req.params.id;
    const result = await RecordServer.delete(userId);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 *批量删除记录
 */
router.post("/bulkDelRecordInfo", async (req, res) => {
  try {
    const result = await RecordServer.bulkDelete(req.body);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 根据分页模糊查询记录
 */
router.get("/getRecord", async (req, res) => {
  try {
    const { page, limit } = req.query;
    const result = await RecordServer.find(page, limit);
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 根据分页模糊查询记录
 */
router.post("/getAllRecordAndPatientInfo", async (req, res) => {
  try {
    const {
      page,
      limit,
      doc_hospital,
      pat_hospital,
      name,
      pos,
      doctor_sn,
      patient_sn,
      createdAt,
      model,
      state,
      sNo,
      sex,
      // realModel
    } = req.body;
    console.log(
      "page limit",
      page,
      limit,
      doc_hospital,
      pat_hospital,
      name,
      pos,
      doctor_sn,
      patient_sn,
      createdAt,
      model,
      state,
      sNo,
      sex,
      // realModel
    );
    const result = await RecordServer.findRecordAndPatientInfo(
      page,
      limit,
      pat_hospital ? pat_hospital : null,
      doc_hospital ? doc_hospital : null,
      name ? name : null,
      pos ? pos : null,
      doctor_sn ? doctor_sn : null,
      patient_sn ? patient_sn : null,
      createdAt ? createdAt : null,
      model ? model : null,
      typeof state == 'number' ? state : null,
      sNo ? sNo : null,
      sex,
      // realModel
    );
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

/**
 * 连表查询未检查的病人及信息
 */
router.get("/findNoCheckedRecord", async (req, res) => {
  try {
    const result = await RecordServer.findNoCheckedRecord();
    ResponseHelper.sendData(result, res);
  } catch (err) {
    console.log("err", err);
    ResponseHelper.sendErr(err, res);
  }
});

module.exports = router;
